package com.fowo.api.syncAmazon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fowo.api.common.model.RException;
import com.fowo.api.entity.ListingFather;
import com.fowo.api.entity.ShopInfo;
import com.fowo.api.entity.TmpListing;
import com.fowo.api.mapper.ListingFatherMapper;
import com.fowo.api.mapper.TmpListingMapper;
import com.fowo.api.syncAmazon.api.ListingService;
import com.fowo.api.syncAmazon.mapper.ListingMapper;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class TmpSyncListingServiceImpl extends ServiceImpl<ListingFatherMapper,ListingFather> {

    @Resource
    private ListingFatherMapper listingFatherMapper;
    @Resource
    private TmpListingMapper tmpListingMapper;
    @Resource
    private ListingMapper listingMapper;
    @Resource
    private ListingService listingService;

    public void syncListing() throws Exception {
        // 清空listing临时表数据
        listingMapper.deleteAll();
        // 拉取listing数据到临时表
        listingService.getListing();
        log.info("拉取listing数据到临时表完成");
        // 插入listing到业务表
//        insertListing();
        listingMapper.updateToListingFather();
        listingMapper.insertToListingFather();
        log.info("插入listing到业务表完成");
    }


    //TODO 字段问题 暂时搁置
    public void insertListing() {
        QueryWrapper<TmpListing> wrapper = new QueryWrapper<>();
        wrapper.eq("del_flag",0);
        List<TmpListing> list = tmpListingMapper.selectList(wrapper);
        if (list == null || list.size() < 1) {
            return;
//            throw new RException("listing数据为空");
        }
        final List<String> listingIdList = list.stream().map(l -> l.getListingId()).collect(Collectors.toList());
        Map<String, List<ListingFather>> collect = null;
        QueryWrapper<ListingFather> queryWrapper = new QueryWrapper<>();
//        queryWrapper.isNotNull("listing_id");
        queryWrapper.in("listing_id",listingIdList);
        List<ListingFather> listingFathers = listingFatherMapper.selectList(queryWrapper);
        if(listingFathers != null && listingFathers.size() > 0){
            collect = listingFathers.stream().collect(Collectors.groupingBy(ListingFather::getListingId));
        }
        List<ListingFather> insertList = new ArrayList<>();
        List<ListingFather> updateList = new ArrayList<>();
        //分批存储
        List<List<TmpListing>> partition = Lists.partition(list, 200);
        for (List<TmpListing> listings:partition) {
            for (TmpListing tmpListing:listings) {
                ListingFather father = new ListingFather();
                father.setCreateTime(new Date());
                father.setDelFlag(false);
                father.setListingId(tmpListing.getListingId());
                father.setMsku(tmpListing.getSellerSku());
                father.setTitle(tmpListing.getName());
                father.setFnsku(tmpListing.getFnsku());
                father.setPrice(tmpListing.getPrice());
//                father.setQuantity(Integer.valueOf(tmpListing.getQuantity()));
                father.setSaleDate(tmpListing.getOpenDate());
                father.setShipping(String.valueOf(tmpListing.getZshopShippingFee()));
                father.setProductRemark(tmpListing.getItemNote());
                father.setAsin(tmpListing.getAsin());
                father.setStatus(tmpListing.getStatus());
                father.setShop(tmpListing.getSid());
                if(collect.get(tmpListing.getListingId()) != null){
                    father.setLastUpdateTime(new Date());
                    updateList.add(father);
                }else {
                    father.setCreateTime(new Date());
                    insertList.add(father);
                }
            }
            this.saveBatch(insertList);
            listingMapper.updateListing(updateList);
        }
    }

    /**
     * 同步某个店铺的Listing
     * @param shopInfo
     * @throws Exception
     */
    public void syncListingByShop(ShopInfo shopInfo) throws Exception {
        // 清空listing临时表数据
        listingMapper.deleteAll();
        // 拉取listing数据到临时表
        listingService.getListingByShop(shopInfo);
        log.info("拉取listing数据到临时表完成");
        // 插入listing到业务表
//        insertListing();
        listingMapper.updateToListingFather();
        listingMapper.insertToListingFather();
        log.info("插入listing到业务表完成");
    }
}
